﻿using _2022Learning._03DesignPattern;
using _2022Learning._04.Delegate;
using _2022Learning._04.Delegate_ObserverPattern;
using System;
using System.Threading;
using System.Threading.Tasks;
using static _2022Learning._03DesignPattern.PrototypePattern;

namespace _2022Learning
{
    class Program
    {
        static void Main(string[] args)
        {

            #region 01.状态设计模式
            //var context = new Context(new ConcreteStateA());
            //context.Request1(); // 执行Request1、Request2的方法，其实是将要执行的内容，委托给了状态类来执行，从而达到对象和行为分离的目的
            //context.Request2();
            #endregion

            #region 02.简易状态机
            //StateMachine2 sm = new StateMachine2();
            //sm.ChangeState(AnimationState.Walk, new StateEvent { Date = "我时是行走的参数" });
            //sm.Update();// 执行行走状态
            //sm.Update();

            //sm.ChangeState(AnimationState.Dead, new StateEvent { Date = "死亡时需要的参数" });
            //sm.Update();
            //sm.isStop = true;
            //sm.Update();
            #endregion

            #region 2.1迭代器
            //foreach (int fib in IteratorModel.Fibs(10))
            //{
            //    Console.WriteLine(fib + " ");
            //}

            //foreach (var item in IteratorModel.Foo(true))
            //{
            //    Console.WriteLine(item);
            //}

            //foreach (Day day in new Days().DaysOfWeek)
            //{
            //    Console.WriteLine("Day {0} of the week is {1}", day.DayOfWeek, day.DayName);
            //}


            //int elementToFind = 21;
            //int maxElements = 100;

            //foreach (int item in IteratorModel.FindFibonacciNumber(elementToFind, maxElements))
            //{
            //    Console.Write("Found the number " + elementToFind + " in Fibonacci series.");
            //}
            #endregion

            #region 03.单例设计模式,多线程请求单例实例
            //var task1 = new Task<SingletonPattern2>(() => SingletonPattern2.GetInstance());
            //task1.RunSynchronously();
            //var model1Task = Task.Run(() => SingletonPattern2.GetInstance());
            //var model2Task = Task.Run(() => SingletonPattern2.GetInstance());
            //var model1Result = model1Task.Result;
            //var model2Result = model2Task.Result;
            //Console.WriteLine(object.ReferenceEquals(model1Result, model2Result));
            #endregion

            #region 04.原型设计模式
            //Person p1 = new Person();
            //p1.Age = 42;
            //p1.BirthDate = Convert.ToDateTime("1977-01-01");
            //p1.Name = "Jack Daniels";
            //p1.IdInfo = new IdInfo(666);

            //Person p2 = p1.ShallowCopy();

            //// 深copy的一种方式：JSON序列化之后在反序列化，就回得到一个新的对象，这个对象就是深Copy出来的
            //Person p3 = p1.DeepCopy();

            //Console.WriteLine("Original values of p1, p2, p3:");
            //Console.WriteLine("   p1 instance values: ");
            //DisplayValues(p1);
            //Console.WriteLine("   p2 instance values:");
            //DisplayValues(p2);
            //Console.WriteLine("   p3 instance values:");
            //DisplayValues(p3);


            //p1.Age = 32;
            //p1.BirthDate = Convert.ToDateTime("1900-01-01");
            //p1.Name = "Frank";
            //p1.IdInfo.IdNumber = 7878;
            //Console.WriteLine("\nValues of p1, p2 and p3 after changes to p1:");
            //Console.WriteLine("   p1 instance values: ");
            //DisplayValues(p1);
            //Console.WriteLine("   p2 instance values (reference values have changed):");
            //DisplayValues(p2);
            //Console.WriteLine("   p3 instance values (everything was kept the same):");
            //DisplayValues(p3);
            #endregion

            #region 05.简单工厂、工厂方法、抽象工厂

            //var mobile = FactoryPattern.GetMobile("小米");// 简单工厂
            //mobile.PhonePrint();

            //var hqFactory = new HQFactory(); // 工厂方法
            //var hqCar = hqFactory.CreatCar();

            //var europeanFactory = new EuropeanFactory(); //不用依赖真实的商品类，从而得到对象

            //var door = europeanFactory.CreateDoor();
            //var floor = europeanFactory.CreateFloor();
            //var roof = europeanFactory.CreateRoof();
            //var window = europeanFactory.CreateWindow();

            #endregion

            #region 06.简单发布订阅模式，借助事件（委托）实现
            //var spoon = new Spoon();
            //var light = new Light();
            //var bell = new Bell();
            //spoon.SubsAction(light.doAction);
            //spoon.SubsAction(bell.doAction);
            //spoon.Boilwater();


            //1、初始化鱼竿
            var fishingRod = new FishingRod();

            //2、声明垂钓者
            var jeff = new FishingMan("圣杰");

            //3.分配鱼竿
            jeff.FishingRod = fishingRod;

            //4、注册观察者,由FishingRod中的构造函数，反射注入
            //fishingRod.FishingEvent += new FishingEventHandler().HandleEvent;

            //5、循环钓鱼
            while (jeff.FishCount < 5)
            {
                jeff.Fishing();
                Console.WriteLine("-------------------");
                //睡眠5s
                Thread.Sleep(5000);
            }

            #endregion

            Console.WriteLine("Hello World!");
            Console.ReadKey();
        }

        #region 私有方法
        public static void DisplayValues(Person p)
        {
            Console.WriteLine("      Name: {0:s}, Age: {1:d}, BirthDate: {2:MM/dd/yy}",
                p.Name, p.Age, p.BirthDate);
            Console.WriteLine("      ID#: {0:d}", p.IdInfo.IdNumber);
        }
        #endregion
    }
}
